#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : gtmsAutoTestProject 
@File    : pos_device_update_status_server.py
@IDE     : PyCharm 
@Author  : Mario
@Date    : 2022/5/7 11:09 
@Describe: POS绑定品牌切换业务实现
"""
from Config.yamlRead import brandCode_cfg
from Comm.logUtils.logControl import LogHandler
from Comm.tools.toolsControl import ToolsHandler
from FastApi.enum.Interface_enum.error_enum import ERROR_ENUM
from APIs.middleApi.setup.pos.pos_manage_api import PosManageAPI

logger = LogHandler().get_logger()


def pos_device_update_status(environment, switchBrandCode, deviceMac):
    """
    POS绑定品牌切换业务实现
    :param environment: 环境
    :param switchBrandCode:设备需要切换到的品牌
    :param deviceMac:设备MAC
    :return:
    """
    brandCode = brandCode_cfg  # 品牌编码列表
    middle_requests = PosManageAPI(environment)
    # 调用设备管理接口
    device_info = middle_requests.pos_device_manage_page(switchBrandCode, deviceMac)
    # 取出需要切换品牌的设备状态,设备状态(0-禁用 1-启用)
    device_status = ToolsHandler.get_json_result(device_info, '$..deviceStatus')
    switch_device_id = ToolsHandler.get_json_result(device_info, '$.data..id')
    logger.info("需要切换的品牌[{}]的设备状态为:[{}]".format(switchBrandCode, device_status))
    if device_status == '0':
        for i in brandCode:  # 循环其他品牌
            if i != switchBrandCode:
                logger.debug("正在循环品牌[{}]的设备信息".format(i))
                device_info = middle_requests.pos_device_manage_page(i, deviceMac)  # 获取当前循环品牌设备信息
                device_status = ToolsHandler.get_json_result(device_info, '$..deviceStatus')  # 获取设备信息的设备状态
                logger.info("循环品牌的设备状态为:[{}]".format(device_status))
                if device_status == '1':
                    device_id = str(ToolsHandler.get_json_result(device_info, '$.data..id'))  # 获取需要关闭的设备ID
                    middle_requests.pos_derive_update_status(brandCode=i, deriveId=device_id, deviceStatus='0')
                    logger.info("正在关闭循环品牌[{}]的设备[{}]".format(i, device_id))
                    break
        middle_requests.pos_derive_update_status(brandCode=switchBrandCode, deriveId=str(switch_device_id), deviceStatus='1')
        result_info = {"code": 200, "message": "以为您切换设备[{}]绑定品牌[{}]".format(deviceMac, switchBrandCode)}
        return result_info
    elif device_status == '1':
        # 设备状态如果为1的话说明在当前环境已经开启，这时候不需要任何处理
        result_info = {"code": ERROR_ENUM.DEVICE_TURNED_ON.value[0], "message": ERROR_ENUM.DEVICE_TURNED_ON.value[1]}
        logger.warning(ERROR_ENUM.DEVICE_TURNED_ON.value[1])
        return result_info
    elif device_status is None:
        # 设备状态获取为空的话，说明当前环境没有对应MAC的设备信息，这时候不需要任何处理
        result_info = {"code": ERROR_ENUM.DEVICE_NOT_FOUND.value[0], "message": ERROR_ENUM.DEVICE_NOT_FOUND.value[1]}
        logger.warning(ERROR_ENUM.DEVICE_NOT_FOUND.value[1])
        return result_info
